package com.perfect.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perfect.base.BaseService;
import com.perfect.dao.ShopMapper;
import com.perfect.dao.UserMapper;
import com.perfect.query.ShopQuery;
import com.perfect.utils.AssertUtil;
import com.perfect.vo.Reconciliation;
import com.perfect.vo.Shop;
import com.perfect.vo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopService extends BaseService<Shop, Integer> {
    @Resource
    private ShopMapper shopMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 返回数据表格指定的数据格式
     *
     * @param query
     * @return
     */
    public Map<String, Object> ListShop(ShopQuery query) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<Shop> pageInfo = new PageInfo<>(shopMapper.selectByParams(query));
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 店铺添加方法
     *
     * @param shop
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addShop(Shop shop) {
        //参数检验
        checkShop(shop);
        //设默认值
        shop.setCreateDate(new Date());
        shop.setIsValid(1);
        //执行添加方法
        AssertUtil.isTrue(shopMapper.insertSelective(shop) < 1, "添加失败!");
    }

    /*
     * 参数检验
     * 店铺名称不能为空
     * 不能重复
     * 用户名称不能为空
     * 不能重复
     * 商铺地址不能为空
     * 是否自营不能为空
     * 店铺状态不能为空
     * */
    private void checkShop(Shop shop) {
        AssertUtil.isTrue(StringUtils.isBlank(shop.getShopName()), "店铺名称为空!");
        AssertUtil.isTrue(null != shopMapper.selectByShopName(shop.getShopName()), "店铺名称被占用");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getMemberName()), "用户名称为空!");
        AssertUtil.isTrue(null != shopMapper.selectByMemberName(shop.getMemberName()), "用户名称被占用");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getShopAddress()), "商铺地址为空!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getSelfSupport()), "是否自营为空!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getShopState()), "店铺状态为空!");

    }

    /**
     * 店铺更新方法
     *
     * @param shop
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateShop(Shop shop) {
        //参数检验
        AssertUtil.isTrue(null == shop.getId(), "修改用户不存在!");
        Shop temp = shopMapper.selectByPrimaryKey(shop.getId());
        AssertUtil.isTrue(null == temp, "修改用户不存在!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getShopName()), "店铺名称为空!");
        AssertUtil.isTrue(null != shopMapper.selectByIdAndShopName(shop.getId(), shop.getShopName()), "店铺名称被占用!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getMemberName()), "用户名称为空!");
        AssertUtil.isTrue(null != shopMapper.selectByIdAndMember(shop.getId(), shop.getMemberName()), "会员名称被占用!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getShopAddress()), "商铺地址为空!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getSelfSupport()), "是否自营为空!");
        AssertUtil.isTrue(StringUtils.isBlank(shop.getShopState()), "店铺状态为空!");

        //设默认值
        shop.setCreateDate(new Date());
        shop.setIsValid(1);
        //执行添加方法
        AssertUtil.isTrue(shopMapper.updateByPrimaryKeySelective(shop) < 1, "添加失败!");
    }

    /**
     * 删除店铺方法
     *
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteShop(Integer[] ids) {
        AssertUtil.isTrue(null == ids, "删除记录为空!");
        AssertUtil.isTrue(ids.length != shopMapper.selectByIds(ids), "删除记录不存在!");
        AssertUtil.isTrue(shopMapper.deleteBatch(ids) < 1, "删除失败!");
    }


    public List<User> queryAllMerchant() {
        return userMapper.queryAllMerchant();
    }

    public Map<String, Object> ListAudit(ShopQuery query) {
        String[] str = new String[2];
        String str1 = null;
        String str2 = null;

        if (!StringUtils.isBlank(query.getCreationTime())) {
            str = query.getCreationTime().split(" - ");
            str1 = str[0];
            str2 = str[1];
        }


        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<Shop> pageInfo = new PageInfo<Shop>(shopMapper.selectByParam(query.getShopName(), query.getMemberName(), str1, str2));
        result.put("count", pageInfo.getTotal());
        result.put("data", pageInfo.getList());
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

}
